﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using WordServices.HttpService;

// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

namespace ForumPages
{
	/// <summary>
	/// An empty page that can be used on its own or navigated to within a Frame.
	/// </summary>
	public sealed partial class ForumHomePage : Page
	{
		//ForumPost post;
		//ForumPost modifiedPost;
		
		List<Forumpost> postss = new List<Forumpost>();
		Forumpost testposts;
		readonly List<OutputData> datas = new List<OutputData>();


		protected override void OnNavigatedTo(NavigationEventArgs e)
		{
			base.OnNavigatedTo(e);

			// Instead of hard coded items, the data could be pulled 
			// asynchronously from a database or the internet.
			/*Contacts.Add(new Contact("John", "Doe", "ABC Printers"));
			Contacts.Add(new Contact("Jane", "Doe", "XYZ Refridgerators"));
			Contacts.Add(new Contact("Santa", "Claus", "North Pole Toy Factory Inc.")); */

		}

		public ForumHomePage()
		{
			this.InitializeComponent();
			/*post = new ForumPost()
			{
				Id = 1,
				UserId = 1,
				Title = "Abrreviation",
				Content = "brreviation可以怎么用",
				CreateTime = DateTime.Now,
				UpdateTime = DateTime.Now
			};
			modifiedPost = new ForumPost()
			{
				Id = 1,
				UserId = 2,
				Title = "ModifiedTitle",
				Content = "ModifiedContent",
				CreateTime = DateTime.Now,
				UpdateTime = DateTime.Now
			};
			posts.Add(post);*/
			testposts = new Forumpost()
			{
				Title = "apple",
				Content = "how apple spell",
				Id = "1",
				Createtime = DateTime.Now.ToString(),

			};
			postss.Add(testposts);
			Refresh();
		}
		private void Forum_ItemClick(object sender, ItemClickEventArgs e)
		{
			var selectpost = (Forumpost)e.ClickedItem;
			this.Frame.Navigate(typeof(FirstPage), selectpost);
		}

		private void Refresh()
		{
			OutputListView.ItemsSource = null;
			OutputListView.ItemsSource = datas;
			PostListView.ItemsSource = null;
			PostListView.ItemsSource = postss;
		}

		private void CreateBtn_Click(object sender, RoutedEventArgs e)
		{
			Forumpost post = new Forumpost()
			{

				Id = IDBox.Text,
				Title = TitleBox.Text,
				Content = ContentBox.Text,
				Createtime = DateTime.Now.ToString(),

			};
			postss.Add(post);
			Refresh();
		  try
				{
					HttpUtils utils = new HttpUtils("https://localhost:44300");
					var msg = utils.SendPostMessage("api/ForumPost/CreatePost", post);
					var task = msg.Content.ReadAsStringAsync();
					task.Wait();
					datas.Add(new OutputData(msg.StatusCode.ToString(), task.Result));
					msg = utils.SendPutMessage("api/ForumPost/CreateTag", new ForumPostTag() { Content = TitleBox.Text, PostId = int.Parse(IDBox.Text) });
					task = msg.Content.ReadAsStringAsync();
					task.Wait();
					datas.Add(new OutputData(msg.StatusCode.ToString(), task.Result));
				}
				catch (Exception ex)
				{
					datas.Add(new OutputData("Fail", ex.ToString()));
				}
				Refresh();

		}

		private void UpdateBtn_Click(object sender, RoutedEventArgs e)
		{

			//	try
			//	{
			//		HttpUtils utils = new HttpUtils("https://localhost:44300");
			Forumpost post = new Forumpost()
			{

				Id = IDBox.Text,
				Title = TitleBox.Text,
				Content = ContentBox.Text,
				Createtime = DateTime.Now.ToString(),

			};
			postss.Add(post);
			//		var msg = utils.SendPutMessage("api/ForumPost/UpdatePost", modifiedPost);
			//		var task = msg.Content.ReadAsStringAsync();
			//		task.Wait();
			//		datas.Add(new OutputData(msg.StatusCode.ToString(), task.Result));
			//	}
			//	catch (Exception ex)
			//	{
			//		datas.Add(new OutputData("Fail", ex.ToString()));
			//	}
			Refresh();

		}

		private void DeleteBtn_Click(object sender, RoutedEventArgs e)
		{
			/*
			try
			{
				HttpUtils utils = new HttpUtils("https://localhost:44300");
				var msg = utils.SendDeleteMessage("api/ForumPost/DeletePost?id=" + DataBox.Text);
				var task = msg.Content.ReadAsStringAsync();
				task.Wait();
				datas.Add(new OutputData(msg.StatusCode.ToString(), task.Result));
			}
			catch (Exception ex)
			{
				datas.Add(new OutputData("Fail", ex.ToString()));
			}
			Refresh();
			*/
		}

		private void RequestBtn_Click(object sender, RoutedEventArgs e)
		{
			/*
			try
			{
				HttpUtils utils = new HttpUtils("https://localhost:44300");
				var msg = utils.SendGetMessage("api/ForumPost/GetAllPosts");
				var task = msg.Content.ReadAsStringAsync();
				task.Wait();
				posts = JsonConvert.DeserializeObject<IEnumerable<ForumPost>>(task.Result).ToList();
				datas.Add(new OutputData(msg.StatusCode.ToString(), task.Result));
			}
			catch (Exception ex)
			{
				datas.Add(new OutputData("Fail", ex.ToString()));
			}
			Refresh();
			*/
		}

		private void RequestByIdBtn_Click(object sender, RoutedEventArgs e)
		{
			/*
			try
			{
				HttpUtils utils = new HttpUtils("https://localhost:44300");
				var msg = utils.SendGetMessage("api/ForumPost/GetPostById?id=" + DataBox.Text);
				var task = msg.Content.ReadAsStringAsync();
				task.Wait();
				datas.Add(new OutputData(msg.StatusCode.ToString(), task.Result));
			}
			catch (Exception ex)
			{
				datas.Add(new OutputData("Fail", ex.ToString()));
			}
			Refresh();
			*/

		}

		private void TextBlock_SelectionChanged(object sender, RoutedEventArgs e)
		{

		}

		private void TitleBox_TextChanged(object sender, TextChangedEventArgs e)
		{

		}
	}

	public class OutputData
	{
		public string Title { get; set; }
		public string Content { get; set; }

		public OutputData() { }

		public OutputData(string title, string content)
		{
			Title = title;
			Content = content;
		}
	}
	public class Forumpost
	{
		public string Id { get; set; }
		public string Title { get; set; }
		public string Content { get; set; }

		public string Createtime { get; set; }


		
		public Forumpost()
		{
		}
	}
	
}
